import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $empty,
  $toast,
  storage,
  Validate
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
const app = getApp();

Page({

  data: {
    allList: [],
    regionList: [],
    value: '',
    searchdata: []
  },

  onChange(e) {
    var regionList = this.data.regionList;
    var allList = [];
    var searchdata = [];
    if (this.data.allList.length == 0) {
      regionList.forEach(item => {
        item.list.forEach(item1 => {
          allList.push(item1);
        });
      })
      this.setData({
        allList: allList
      });
    } else
      allList = this.data.allList;
    allList.forEach(item => {
      if (e.detail != "") {
        if (item.name.indexOf(e.detail) > -1) {
          searchdata.push(item);
        }
      }
    });
    if (e.detail == "")
      searchdata = [];
    this.setData({
      value: e.detail,
      allList: allList,
      searchdata: searchdata
    });


  },
  onSearch() {
    Toast('搜索' + this.data.value);
  },
  /**
   * 获取地区
   */
  getRegionInfo() {
    return new Promise((resolve) => {
      storage.get('regionList').then((res) => {
        this.setData({
          regionList: res
        });
        resolve(res);
      }).catch((res) => {
        console.log('2323432')
        http.get(api.default, {
          data: {
            action: 'getlocation'
          }
        }, false).then((res) => {
          const {
            data
          } = res.data;
          this.setData({
            regionList: data
          });
          storage.set('regionList', data);
          resolve(data);
        });
      });
    });
  },
  setzone(e) {
    const data = e.currentTarget.dataset || e.target.dataset;
    app.globalData.zoneid = data.id;
    //获取设置城市id
    http.post(api.default, {
      data: {
        action: 'getregioninfo',
        id: app.globalData.zoneid
      }
    }, false).then((res) => {
      const {
        data
      } = res.data;
      if (data != null) {
        app.globalData.zone = data;
        //存在上一页,关闭当前返回一层,//没有上一页，跳回首页
        wxApi.refreshPage(getPage.home);
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //获取路由数量
    this.setData({
      getPages: getCurrentPages().length
    })
    this.getRegionInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})